package cn.com.eado.crm.v.weiget

import android.app.Activity
import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ArrayAdapter
import android.widget.LinearLayout
import androidx.appcompat.widget.ListPopupWindow
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.navigation.Navigation
import cn.com.eado.crm.R
import kotlinx.android.synthetic.main.title_bar.view.*
import androidx.core.content.ContextCompat.getSystemService
import android.view.inputmethod.InputMethodManager
import android.view.inputmethod.EditorInfo
import cn.com.eado.crm.app.extenstion.toast


/**
 * app title bar
 */
class TitleBar(private val ctx: Context, private val attributeSet: AttributeSet?, defStyleAttr: Int) :
    ConstraintLayout(ctx, attributeSet, defStyleAttr) {
    constructor(ctx: Context, attributeSet: AttributeSet?) : this(ctx, attributeSet, 0)
    constructor(ctx: Context) : this(ctx, null)

//    private var mListPop: ListPopupWindow? = null
//    private var isShow = false
//    private val stringList by lazy { mutableListOf<String>() }
    var tvRightClickCallback: (() -> Unit)? = null
//    var ivSearchClickCallback: (() -> Unit)? = null
//    var ivAddClickCallback: (() -> Unit)? = null
//    var ivAddContactClickCallback: (() -> Unit)? = null
    var searchCallback: ((s: String) -> Unit)? = null

    init {
        LayoutInflater.from(ctx).inflate(R.layout.title_bar, this)
        //get property value
        val typedArray: TypedArray = ctx.obtainStyledAttributes(attributeSet, R.styleable.TitleBar)
        val title = typedArray.getString(R.styleable.TitleBar_title)
        val stationVisible = typedArray.getBoolean(R.styleable.TitleBar_station_visible, false)
        val station = typedArray.getString(R.styleable.TitleBar_station)
        val cbVisible = typedArray.getBoolean(R.styleable.TitleBar_checkbox_visible, false)
        val searchVisible = typedArray.getBoolean(R.styleable.TitleBar_search_visible, false)
        val addVisible = typedArray.getBoolean(R.styleable.TitleBar_add_visible, false)
        val addContactVisible = typedArray.getBoolean(R.styleable.TitleBar_add_contact_visible, false)
//        val cbBackground =  typedArray.getResourceId(R.styleable.TitleBar_checkbox_bg, R.drawable.selector_checkbox_bg1)
        //set view
        tvTitle.text = title

        search.visibility = if (searchVisible) View.VISIBLE else View.GONE
        search.setOnEditorActionListener{v, actionId, event ->
            if (actionId === EditorInfo.IME_ACTION_SEARCH) {
                val s = search.text.toString().trim()
                if(s.isNotEmpty()){
                    /*隐藏软键盘*/
                    val imm = search.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                    if (imm.isActive) {
                        imm.hideSoftInputFromWindow(search.applicationWindowToken, 0)
                    }
                    searchCallback?.invoke(s)
                }else{
                    ctx as Activity
                    ctx.toast("输入不能为空")
                }
            }
            false
        }

        tvStation.text = station
        tvStation.visibility = if (stationVisible) View.VISIBLE else View.GONE
        tvStation.setOnClickListener { tvRightClickCallback?.invoke() }
//        cb.setBackgroundResource(cbBackground)
//        ivSearch.visibility = if (cbVisible) View.VISIBLE else View.GONE
//        ivSearch.setOnClickListener { ivSearchClickCallback?.invoke() }
//        ivAdd.visibility = if (addVisible) View.VISIBLE else View.GONE
//        ivAdd.setOnClickListener { ivAddClickCallback?.invoke() }
//        ivAddContact.visibility = if (addContactVisible) View.VISIBLE else View.GONE
//        ivAddContact.setOnClickListener { ivAddContactClickCallback?.invoke() }

        ivBack.setOnClickListener { Navigation.findNavController(it).popBackStack() }
    }

//    fun setCheckboxBg(resId: Int){
//        cb.setBackgroundResource(resId)
//    }
//
    fun setTitle(title: String){
        if (tvTitle.isVisible) tvTitle.text = title
    }

    fun getTitle(): String{
        return tvTitle.text.toString()
    }

    fun setHint(s: String){
        search.hint = s
    }
//
//    fun setText(text: String){
//        if (tvStation.isVisible) tvStation.text = text
//    }
//
//    fun setList(list: List<String>){
//        stringList.clear()
//        if (list.isNotEmpty()) stringList.addAll(list)
//    }

//    private fun showData(){
//        isShow = true
//        cb.isChecked = true
//        if (mListPop == null) mListPop = ListPopupWindow(ctx)
//        mListPop?.setAdapter(ArrayAdapter(ctx, R.layout.my_simple_list_item, stringList))
//        mListPop?.width = LinearLayout.LayoutParams.WRAP_CONTENT
//        mListPop?.height = LinearLayout.LayoutParams.WRAP_CONTENT//resources.displayMetrics.heightPixels/2
//        mListPop?.anchorView = layout//设置ListPopupWindow的锚点，即关联PopupWindow的显示位置和这个锚点
//        mListPop?.isModal = true//设置是否是模式
//
//        mListPop?.setOnItemClickListener { parent, view, position, id ->
//            if (position >= 1) {
//                tvStation.text = stringList[position]
//                clickItemCallback.invoke(position-1)
//            }
//            mListPop?.dismiss()
//        }
//
//        mListPop?.setOnDismissListener {
//            isShow = false
//            cb.isChecked = false
//        }
//
//        mListPop?.show()
//    }
//
//    fun setCheckBoxButton(resId: Int){
//        cb.setBackgroundResource(resId)
//    }
}